home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / Cache / Container / dbx.php < prev    next >
PHP Script  |  2004-10-01  |  10KB  |  303 lines

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PEAR :: Cache                                                        |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1997-2003 The PHP Group                                |
  6. // +----------------------------------------------------------------------+
  7. // | This source file is subject to version 2.0 of the PHP license,       |
  8. // | that is bundled with this package in the file LICENSE, and is        |
  9. // | available at through the world-wide-web at                           |
  10. // | http://www.php.net/license/2_02.txt.                                 |
  11. // | If you did not receive a copy of the PHP license and are unable to   |
  12. // | obtain it through the world-wide-web, please send a note to          |
  13. // | license@php.net so we can mail you a copy immediately.               |
  14. // +----------------------------------------------------------------------+
  15. // | Authors: Christian Stocker <chregu@phant.ch>                         |
  16. // +----------------------------------------------------------------------+
  17. //
  18. // $Id: dbx.php,v 1.4 2003/01/04 11:54:46 mj Exp $
  19.  
  20.  
  21. require_once 'Cache/Container.php';
  22.  
  23. /**
  24. * ext/dbx Cache Container.
  25. *
  26. * WARNING: Other systems might or might not support certain datatypes of 
  27. * the tables shown. As far as I know there's no large binary 
  28. * type in SQL-92 or SQL-99. Postgres seems to lack any 
  29. * BLOB or TEXT type, for MS-SQL you could use IMAGE, don't know 
  30. * about other databases. Please add sugestions for other databases to 
  31. * the inline docs.
  32. *
  33. * The field 'changed' has no meaning for the Cache itself. It's just there 
  34. * because it's a good idea to have an automatically updated timestamp
  35. * field for debugging in all of your tables.
  36. *
  37. * For _MySQL_ you need this DB table:
  38. *
  39. * CREATE TABLE cache (
  40. *   id          CHAR(32) NOT NULL DEFAULT '',
  41. *   cachegroup  VARCHAR(127) NOT NULL DEFAULT '',
  42. *   cachedata   BLOB NOT NULL DEFAULT '',
  43. *   userdata    VARCHAR(255) NOT NULL DEFAULT '',
  44. *   expires     INT(9) NOT NULL DEFAULT 0,
  45. *  
  46. *   changed     TIMESTAMP(14) NOT NULL,
  47. *  
  48. *   INDEX (expires),
  49. *   PRIMARY KEY (id, cachegroup)
  50. * )
  51. *
  52. * @author   Christian Stocker <chregu@phant.ch>
  53. * @version  $Id: dbx.php,v 1.4 2003/01/04 11:54:46 mj Exp $
  54. * @package  Cache
  55. */
  56. class Cache_Container_dbx extends Cache_Container {
  57.  
  58.     /**
  59.     * Name of the DB table to store caching data
  60.     * 
  61.     * @see  Cache_Container_file::$filename_prefix
  62.     */  
  63.     var $cache_table = '';
  64.  
  65.     /**
  66.     * DBx module to use
  67.     *
  68.     *  at the moment only mysql or odbc
  69.     * 
  70.     * @var  string
  71.     */
  72.     var $module = '';
  73.  
  74.     /**
  75.     * DB host to use
  76.     * 
  77.     * @var  string
  78.     */
  79.     var $host = '';
  80.  
  81.     /**
  82.     * DB database to use
  83.     * 
  84.     * @var  string
  85.     */
  86.     var $db = '';
  87.  
  88.     /**
  89.     * DB username to use
  90.     * 
  91.     * @var  string
  92.     */
  93.     var $username = '';
  94.  
  95.     /**
  96.     * DB password to use
  97.     * 
  98.     * @var  string
  99.     */
  100.     var $password = '';
  101.  
  102.     /**
  103.     * DBx handle object
  104.     * 
  105.     * @var  object DBx handle
  106.     */
  107.     var $db;
  108.     
  109.     
  110.     /**
  111.     * Establish a persistent connection?
  112.     * 
  113.     * @var  boolean 
  114.     */
  115.     var $persistent = true;
  116.     
  117.  
  118.     function Cache_Container_dbx($options)
  119.     {
  120.         if (!is_array($options) ) {
  121.             return new Cache_Error('No options specified!', __FILE__, __LINE__);
  122.         }
  123.  
  124.         $this->setOptions($options,  array_merge($this->allowed_options, array('module','host','db','username','password', 'cache_table', 'persistent')));
  125.  
  126.         if (!$this->module)
  127.             return new Cache_Error('No module specified!', __FILE__, __LINE__);
  128.  
  129.         $this->db = dbx_connect($this->module, $this->host, $this->db, $this->username, $this->password, $this->persistent);
  130.  
  131.         if (dbx_error($this->db)) {
  132.             return new Cache_Error('DBx connect failed: ' . dbx_error($this->db), __FILE__, __LINE__);
  133.         } else {
  134.             //not implemented yet in dbx
  135.             //$this->db->setFetchMode(DB_FETCHMODE_ASSOC);
  136.         }
  137.     }
  138.  
  139.     function fetch($id, $group)
  140.     {
  141.         $query = sprintf("SELECT cachedata, userdata, expires FROM %s WHERE id = '%s' AND cachegroup = '%s'",
  142.                          $this->cache_table,
  143.                          addslashes($id),
  144.                          addslashes($group)
  145.                         );
  146.  
  147.         $res = dbx_query($this->db, $query);
  148.         if (dbx_error($this->db))
  149.             return new Cache_Error('DBx query failed: ' . dbx_error($this->db), __FILE__, __LINE__);
  150.  
  151.         $row = $res->data[0];
  152.  
  153.         if (is_array($row))
  154.             $data = array($row['expires'], $this->decode($row['cachedata']), $row['userdata']);
  155.         else 
  156.             $data = array(NULL, NULL, NULL);
  157.  
  158.         // last used required by the garbage collection   
  159.         // WARNING: might be MySQL specific         
  160.         $query = sprintf("UPDATE %s SET changed = (NOW() + 0) WHERE id = '%s' AND cachegroup = '%s'",
  161.                             $this->cache_table,
  162.                             addslashes($id),
  163.                             addslashes($group)
  164.                           );
  165.         
  166.         $res = dbx_query($this->db, $query);
  167.         if (dbx_error($this->db))
  168.             return new Cache_Error('DBx query failed: ' . dbx_error($this->db), __FILE__, __LINE__);                             
  169.             
  170.         return $data;            
  171.     }
  172.  
  173.     /**
  174.     * Stores a dataset.
  175.     * 
  176.     * WARNING: we use the SQL command REPLACE INTO this might be 
  177.     * MySQL specific. As MySQL is very popular the method should
  178.     * work fine for 95% of you.
  179.     */
  180.     function save($id, $data, $expires, $group, $userdata)
  181.     {
  182.         $this->flushPreload($id, $group);
  183.  
  184.         $query = sprintf("REPLACE INTO %s (userdata, cachedata, expires, id, cachegroup) VALUES ('%s', '%s', %d, '%s', '%s')",
  185.                          $this->cache_table,
  186.                          addslashes($userdata),
  187.                          addslashes($this->encode($data)),
  188.                          $this->getExpiresAbsolute($expires) ,
  189.                          addslashes($id),
  190.                          addslashes($group)
  191.                         );
  192.  
  193.         $res = dbx_query($this->db, $query);
  194.  
  195.         if (dbx_error($this->db)) {
  196.             return new Cache_Error('DBx query failed: ' . dbx_error($this->db) , __FILE__, __LINE__);
  197.         }
  198.     }
  199.  
  200.     function remove($id, $group)
  201.     {
  202.         $this->flushPreload($id, $group);
  203.  
  204.         $query = sprintf("DELETE FROM %s WHERE id = '%s' and cachegroup = '%s'",
  205.                          $this->cache_table,
  206.                          addslashes($id),
  207.                          addslashes($group)
  208.                         );
  209.  
  210.         $res = dbx_query($this->db, $query);
  211.  
  212.         if (dbx_error($this->db))
  213.             return new Cache_Error('DBx query failed: ' . dbx_error($this->db), __FILE__, __LINE__);
  214.     }
  215.  
  216.     function flush($group = '')
  217.     {
  218.         $this->flushPreload();
  219.  
  220.         if ($group) {
  221.             $query = sprintf("DELETE FROM %s WHERE cachegroup = '%s'", $this->cache_table, addslashes($group));
  222.         } else {
  223.             $query = sprintf("DELETE FROM %s", $this->cache_table);
  224.         }
  225.  
  226.         $res = dbx_query($this->db,$query);
  227.  
  228.         if (dbx_error($this->db))
  229.             return new Cache_Error('DBx query failed: ' . dbx_error($this->db), __FILE__, __LINE__);
  230.     }
  231.  
  232.     function idExists($id, $group)
  233.     {
  234.         $query = sprintf("SELECT id FROM %s WHERE ID = '%s' AND cachegroup = '%s'",
  235.                          $this->cache_table,
  236.                          addslashes($id),
  237.                          addslashes($group)
  238.                         );
  239.  
  240.         $res = dbx_query($this->db, $query);
  241.  
  242.         if (dbx_error($this->db))
  243.             return new Cache_Error('DBx query failed: ' . dbx_error($this->db), __FILE__, __LINE__);
  244.  
  245.  
  246.         $row = $res[0];
  247.  
  248.         if (is_array($row)) {
  249.             return true;
  250.         } else {
  251.             return false;
  252.         }
  253.     }
  254.  
  255.     function garbageCollection($maxlifetime)
  256.     {
  257.         $this->flushPreload();
  258.         
  259.         $query = sprintf('DELETE FROM %s WHERE (expires <= %d AND expires > 0) OR changed <= (NOW() - %d)',
  260.                          $this->cache_table,
  261.                          time(),
  262.                          $maxlifetime
  263.                        );
  264.  
  265.  
  266.         $res = dbx_query($this->db, $query);
  267.  
  268.         if (dbx_error($this->db))
  269.             return new Cache_Error('DBx query failed: ' . dbx_error($this->db), __FILE__, __LINE__);
  270.  
  271.         $query = sprintf('select sum(length(cachedata)) as CacheSize from %s',
  272.                          $this->cache_table
  273.                        );
  274.  
  275.         $res = dbx_query($this->db, $query);
  276.         //if cache is to big.
  277.         if ($res->data[0][CacheSize] > $this->highwater)
  278.         {
  279.             //find the lowwater mark.
  280.             $query = sprintf('select length(cachedata) as size, changed from %s order by changed DESC',
  281.                                      $this->cache_table
  282.                        );
  283.  
  284.             $res = dbx_query($this->db, $query);
  285.             $keep_size=0;
  286.             $i=0;
  287.             while ($keep_size < $this->lowwater && $i < $res->rows )
  288.             {
  289.  
  290.                 $keep_size += $res->data[$i][size];
  291.                 $i++;
  292.     }
  293.     
  294.             //delete all entries, which were changed before the "lowwwater mark"
  295.             $query = sprintf('delete from %s where changed <= %s',
  296.                                      $this->cache_table,
  297.                                      $res->data[$i][changed]
  298.                                    );
  299.             $res = dbx_query($this->db, $query);
  300.         }
  301.     }
  302. }
  303. ?>